/*
 * Copyright (c) Jan Šlahora (${USER}) - 2012.
 * Created in IntelliJ IDEA 10.5
 */

package statistics.tests;

import statistics.Asonance.IAsonance;

import java.util.Collections;

/*
*   File Bartlett 
*   in package statistics.tests
*   was created by slaha
*   on 27.3.12 at 14:56.
*
*/
public class Bartlett {
  private double C, B;

  public Bartlett(IAsonance aTest) {
    this(aTest.getaTest());
  }

  public Bartlett(ATest aTest) {
    this.C = vypoctiC(aTest);
    this.B = vypoctiB(aTest);
  }
  private double vypoctiB(ATest aTest) {
    double lokal_B = 1d / this.C;

    double n = aTest.getN();
    int k = aTest.getK();

    //double prumerS2 = aTest.dejCelkoveSi2();
    double prumerS2 = si2(aTest, k);
    prumerS2 = Math.log(prumerS2);
    prumerS2 *= (n-k);

    double suma = 0d;
    for (int i = 0; i < k; i++) {
      double zlomek = (aTest.getNi(i) - 1d);
      zlomek *= aTest.getlnSi2(i);
      suma += zlomek;
    }

    double zavorka = prumerS2 -suma;

    return (lokal_B * zavorka);
  }

  private double vypoctiC(ATest aTest) {
    double lokal_C;

    int k = aTest.getK();
    double n = aTest.getN();
    lokal_C = (1d/(3d * (k - 1d)));


    double zavorka = 0d;
    for (int i = 0; i < k; i++) {
      double zlomek = 1d / (aTest.getNi(i) - 1);
      zavorka += zlomek;
    }

    double zlomek2 = 1d / (n - k);
    zavorka -= zlomek2;

    //zavorka -= zlomek2;
    return (1+(lokal_C * zavorka));
  }


  public double getB() {
    return B;
  }

  private double si2(ATest a, int k) {
    double suma = 0d;
    for (int i = 0; i < k; i++) {
      suma += a.getSi2(i);
    }
    return (suma / k);
  }
}
